﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.admin
{
    public partial class ManageTypes : System.Web.UI.Page
    {
        public TypesDAL dal = new TypesDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindDropDownList();
                this.BindTreeView();
            }

        }
        public void BindDropDownList()
        {
            this.DropDownList1.DataSource = dal.GetAllTypes();
            this.DropDownList1.DataTextField = "typeName";
            this.DropDownList1.DataValueField = "typeId";
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0, new ListItem("顶级规格指标", "0"));
            this.DropDownList1.SelectedIndex = 0;

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int parentId = int.Parse(this.DropDownList1.SelectedValue.ToString());
            string typeName = this.TextBoxNames.Text.Trim();

            int check = dal.CheckTypesExsits(typeName, parentId);
            if (check > 0)
            {
                this.Label1.Visible = true;
                this.Label1.Text = "此规格已经存在！";
            }
            else
            {
                this.Label1.Visible = false;
                int temp = dal.AddTypes(parentId, typeName);
                if (temp > 0)
                {
                    this.BindDropDownList();
                    this.BindTreeView();

                }
            }
        }

        public void BindTreeView()
        {
            this.showTree();

        }

        #region----------------动态树---------------------
        protected void showTree()
        {
            this.TreeView1.Nodes.Clear();//清除节点
            TreeNode node = new TreeNode();//实例化树节点对象
            node.Text = "产品规格指标管理";//添加节点文本
            node.Value = "0";//添加节点值
            node.ImageUrl = "../images/tree/folder_closed.gif";//设置节点样式
            this.TreeView1.Nodes.Add(node);//将节点对象的属性添加到TreeView控件
            this.showTreeView(node);


        }

        protected void showTreeView(TreeNode node)//添加根节点
        {

            int Id = int.Parse(node.Value.ToString());//调用节点的值
            List<TypesInfo> list = dal.GetAllTypesTreeView(Id);//通过父节点的值查出相应的子节点名称（类别名称）
            foreach (TypesInfo ti in list)
            {
                TreeNode tn = new TreeNode();//实例化树节点对象
                //tn.Target = "mainFrame";// ti.Target;

                //tn.NavigateUrl = ti.Url;

                tn.Text = ti.TypeName;// +"ID：" + ti.Id + " URL：" + ti.Url + " 序号：" + ti.Sequence.ToString();//设置节点文本
                tn.Value = ti.TypeId.ToString();//设置节点值

                if (Convert.ToInt32(Id) == 0)
                {
                    tn.ImageUrl = "../images/tree/folder_closed.gif";//为一级和二级节点就显示此节点样式
                }
                else
                {
                    tn.ImageUrl = "../images/tree/tree_node.gif";

                }
                node.ChildNodes.Add(tn);//将上一级节点作为父节点
                showTreeView(tn);//递归调用
            }
        }
        #endregion

        protected void btnAlter_Click(object sender, EventArgs e)
        {
            int typeId = int.Parse(this.TextBoxDeptId.Text);
            string typeName = this.TextBoxNames.Text;
            int parentId = int.Parse(this.DropDownList1.SelectedValue.ToString());
            int temp = dal.UpdateType(typeId, typeName, parentId);
            if (temp > 0)
            {
                this.Label1.Text = "修改成功！";
                this.BindTreeView();

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int typeId = int.Parse(this.TextBoxDeptId.Text);

            int temp = dal.DeleteType(typeId);
            if (temp > 0)
            {
                this.Label1.Text = "删除成功！";
                this.BindTreeView();
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            this.TextBoxDeptId.Text = this.TreeView1.SelectedNode.Value.ToString();
            int typeId = int.Parse(this.TreeView1.SelectedNode.Value.ToString());
            this.GetSelectedNodeInfo(typeId);
        }
        public void GetSelectedNodeInfo(int id)
        {
            if (id > 0)
            {
                DataRow dr = dal.GetTypesInfo(id).Tables[0].Rows[0];
                this.TextBoxNames.Text = dr[1].ToString();
                this.TextBoxDeptId.Text = dr[0].ToString();
                this.Label1.Visible = false;
                this.DropDownList1.SelectedValue = dr[2].ToString();
                //this.TextBoxParentId.Text = dr[2].ToString();

            }

        }

    }
}